﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.ComponentModel.DataAnnotations;


namespace LiveProjectManager.DDD.Domains
{
    [Flags]
    public enum TaskPriority : byte
    { 
        LOW = 0,
        MEDIUM = 1,
        HIGH = 2,
        URGENT = 3
    }


    public class Task : Base.Domain
    {
        // Constants.
        public const TaskPriority DEFAULT_PRIORITY = TaskPriority.MEDIUM;
        public const int MINIMAL_PROGRESSION = 0;
        public const int MAXIMAL_PROGRESSION = 100;
        public const int TITLE_MAXLENGTH = 20;
        
        // Primary key of a task.
        [Key]
        [DatabaseGenerated(DatabaseGeneratedOption.Identity)]
        public long Id { get; set; }

        // The task title.
        [Required]
        [StringLength(TITLE_MAXLENGTH)]
        public string Title { get; set; }

        // The current priority (stored label) of a task.
        [Required]
        public string PriorityLabel { get; set; }

        // The TaskPriority Object corresponding to stored label.
        [NotMappedAttribute]
        public TaskPriority Priority { 
            get { return (TaskPriority)Enum.Parse(typeof(TaskPriority), PriorityLabel, true); } 
            set { PriorityLabel = Enum.GetName(typeof(TaskPriority), value); } 
        }

        // The current progression of a task.
        [Required]
        [Range(MINIMAL_PROGRESSION, MAXIMAL_PROGRESSION)]
        public int Progression { get; set; }

        // At the moment we only support one assignee per task.
        [Required]
        public virtual User User { get; set; }

        // This should create ntext column in Sql Server and not nvarchar.
        [Column(TypeName = "ntext")]
        [MaxLength]
        public string Description { get; set; }

        // The date the task was created on.
        public DateTime CreatedOn { get; set; }


        override public Dictionary<string, object> toDict()
        {
            Dictionary<string, object> myDict = new Dictionary<string, object>();
            myDict.Add("Id", Id);
            myDict.Add("Title", Title);
            myDict.Add("Priority", PriorityLabel);
            myDict.Add("Progression", Progression);
            myDict.Add("Description", Description);
            myDict.Add("Assignee", User.toDict());
            myDict.Add("CreatedOn", CreatedOn);
            return myDict;
        }
    }
}
